{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Flipping an Image"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: flipAndInvertImage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #翻转图像"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个<meta charset=\"UTF-8\" />&nbsp;<code>n x n</code>&nbsp;的二进制矩阵&nbsp;<code>image</code>&nbsp;，先 <strong>水平</strong> 翻转图像，然后&nbsp;<strong>反转&nbsp;</strong>图像并返回&nbsp;<em>结果</em>&nbsp;。</p>\n",
    "\n",
    "<p><strong>水平</strong>翻转图片就是将图片的每一行都进行翻转，即逆序。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，水平翻转&nbsp;<code>[1,1,0]</code>&nbsp;的结果是&nbsp;<code>[0,1,1]</code>。</li>\n",
    "</ul>\n",
    "\n",
    "<p><strong>反转</strong>图片的意思是图片中的&nbsp;<code>0</code>&nbsp;全部被&nbsp;<code>1</code>&nbsp;替换，&nbsp;<code>1</code>&nbsp;全部被&nbsp;<code>0</code>&nbsp;替换。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，反转&nbsp;<code>[0,1,1]</code>&nbsp;的结果是&nbsp;<code>[1,0,0]</code>。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>image = [[1,1,0],[1,0,1],[0,0,0]]\n",
    "<strong>输出：</strong>[[1,0,0],[0,1,0],[1,1,1]]\n",
    "<strong>解释：</strong>首先翻转每一行: [[0,1,1],[1,0,1],[0,0,0]]；\n",
    "     然后反转图片: [[1,0,0],[0,1,0],[1,1,1]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]\n",
    "<strong>输出：</strong>[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\n",
    "<strong>解释：</strong>首先翻转每一行: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]]；\n",
    "     然后反转图片: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<p><meta charset=\"UTF-8\" /></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == image.length</code></li>\n",
    "\t<li><code>n == image[i].length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 20</code></li>\n",
    "\t<li><code>images[i][j]</code>&nbsp;==&nbsp;<code>0</code>&nbsp;或&nbsp;<code>1</code>.</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [flipping-an-image](https://leetcode.cn/problems/flipping-an-image/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [flipping-an-image](https://leetcode.cn/problems/flipping-an-image/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,1,0],[1,0,1],[0,0,0]]', '[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n=len(image)\n",
    "        for i in range(n):\n",
    "            image[i]=[1-image[i][k] for k in range(n-1,-1,-1)]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for a in image:\n",
    "            a.reverse()\n",
    "            for i in range(len(a)):\n",
    "                a[i]^=1\n",
    "        return image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i].reverse()\n",
    "            for j in range(len(image[i])):\n",
    "                image[i][j] ^= 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i] = image[i][-1::-1]\n",
    "            for j in range(len(image[i])):\n",
    "                image[i][j] ^= 1\n",
    "        return image\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in image:\n",
    "            n = len(i)\n",
    "            for j in range(n//2):\n",
    "                i[j],i[n-1-j] = i[n-1-j],i[j]\n",
    "        for i in image:\n",
    "            for j in range(len(i)):\n",
    "                if i[j] == 1:\n",
    "                    i[j] = 0\n",
    "                else:\n",
    "                    i[j] = 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i].reverse()\n",
    "            for j in range(len(image[0])):\n",
    "                image[i][j] = 1 - image[i][j]                    \n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            #list_ = image[i][-1:]\n",
    "            list_ = []\n",
    "            for j in range(len(image[i])- 1, -1, -1):\n",
    "                if image[i][j] == 0:\n",
    "                    list_.append(1)\n",
    "                else:\n",
    "                    list_.append(0)\n",
    "            image[i] = list_\n",
    "        return image\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        r, c = len(image), len(image[0])\n",
    "        for i in range(c):\n",
    "            image[i].reverse()\n",
    "            for j in range(c):\n",
    "                image[i][j] ^= 1\n",
    "        return image\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i].reverse()\n",
    "            for j in range(len(image[i])):\n",
    "                image[i][j] = 0 if image[i][j] == 1 else 1\n",
    "        return image\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        return [[i ^ 1 for i in temp[::-1]]for temp in A]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        for row in A:       \n",
    "            for k,_ in enumerate(row): row[k] = 1 - row[k]        # Python 风格的循环, 1和0反转\n",
    "            i, j = 0, len(row) - 1\n",
    "            while i < j:\n",
    "                row[i], row[j] = row[j], row[i] # Python 特有的交换\n",
    "                i, j = i + 1, j - 1\n",
    "        return A\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in image:\n",
    "            i.reverse()\n",
    "            for j in range(len(i)):\n",
    "                i[j] ^= 1 \n",
    "\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        for row in A:       \n",
    "            for k,_ in enumerate(row): row[k] = 1 - row[k]        \n",
    "            i, j = 0, len(row) - 1\n",
    "            while i < j:\n",
    "                row[i], row[j] = row[j], row[i]\n",
    "                i, j = i + 1, j - 1\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            a = image[i][::-1]\n",
    "            \n",
    "            \n",
    "            for j in range(len(a)):\n",
    "                if a[j] == 0:\n",
    "                    a[j] = 1\n",
    "                else:\n",
    "                    a[j] = 0\n",
    "            image[i] = a\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        for i in range(n):\n",
    "            left, right = 0, n - 1\n",
    "            while left < right:\n",
    "                if image[i][left] == image[i][right]:\n",
    "                    image[i][left] ^= 1\n",
    "                    image[i][right] ^= 1\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if left == right:\n",
    "                image[i][left] ^= 1\n",
    "        return image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def flipAndInvertImage(self, A):\n",
    "        for i in range(len(A)):\n",
    "            left,right = 0, len(A) - 1\n",
    "            while left < right:\n",
    "                if A[i][left] == A[i][right]:\n",
    "                    A[i][left] ^= 1\n",
    "                    A[i][right] ^= 1\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if left == right:   #存在元素为奇数个，别忘了中间元素反转\n",
    "                A[i][left] ^= 1\n",
    "        return A\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        rows = len(image)\n",
    "        cols = len(image[0])\n",
    "\n",
    "        for row in range(rows):\n",
    "            image[row] = image[row][::-1]\n",
    "            for col in range(cols):\n",
    "                image[row][col] ^=1 \n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "      for i in range(len(image)):\n",
    "        image[i] = [1^x for x in image[i]][::-1]\n",
    "      return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        tmp = copy.deepcopy(image)\n",
    "        for j in range(n):\n",
    "            for i in range(n):\n",
    "                local = tmp[j][n - 1 - i]\n",
    "                if local == 1:\n",
    "                    local = 0\n",
    "                else:\n",
    "                    local = 1\n",
    "                image[j][i] = local\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for arr in image:\n",
    "            arr.reverse()\n",
    "            for i in range(0,len(arr)):\n",
    "                arr[i]^=1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        for i in range(n):\n",
    "            j, k = 0, n-1\n",
    "            while j<k:\n",
    "                if image[i][j] == image[i][k]:\n",
    "                    image[i][j] ^= 1\n",
    "                    image[i][k] ^= 1\n",
    "                j+=1\n",
    "                k-=1\n",
    "            if j==k:\n",
    "                image[i][j] ^= 1\n",
    "        return image\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        #image = [x[::-1] for x in image]\n",
    "        image = [[1-j for j in x[::-1]] for x in image]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        ans=list()\n",
    "        mem=list()\n",
    "        for i in image:\n",
    "            a=i[::-1]\n",
    "            for item in a:\n",
    "                mem.append(int(not bool(item)))\n",
    "            ans.append(mem)\n",
    "            mem=[]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        def reverse(l):\n",
    "            l = l[::-1]\n",
    "            for i in range(len(l)):\n",
    "                if l[i] == 1:\n",
    "                    l[i] = 0\n",
    "                else:\n",
    "                    l[i] = 1\n",
    "            return l\n",
    "        list_image = []\n",
    "        for im in image:\n",
    "            i = reverse(im)\n",
    "            list_image.append(i)\n",
    "\n",
    "        return list_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        return [[j^1 for j in row[::-1]] for row in image]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            tmp = []\n",
    "            cur = image[i][::-1]\n",
    "            for j in range(n):\n",
    "                if cur[j] == 1:\n",
    "                    tmp.append(0)\n",
    "                else:\n",
    "                    tmp.append(1)\n",
    "            res.append(tmp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        ans=list()\n",
    "        mem=list()\n",
    "        for i in image:\n",
    "            a=i[::-1]\n",
    "            for item in a:\n",
    "                mem.append(int(not bool(item)))\n",
    "            ans.append(mem)\n",
    "            mem=[]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        row = len(image)\n",
    "        colum = len(image[0])\n",
    "\n",
    "        for i in range(row):\n",
    "            image[i].reverse()\n",
    "        \n",
    "        for i in range(row):\n",
    "            for j in range(colum):\n",
    "                if image[i][j] == 1:\n",
    "                    image[i][j] = 0\n",
    "                elif image[i][j] == 0:\n",
    "                    image[i][j] = 1\n",
    "        \n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i]=image[i][::-1]\n",
    "\n",
    "        for i in range(len(image)):\n",
    "            for j in range(len(image[i])):\n",
    "                if image[i][j]==0:\n",
    "                    image[i][j]=1\n",
    "                else:\n",
    "                    image[i][j]=0\n",
    "\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        r, c = len(image), len(image[0])\n",
    "        for i in range(r):\n",
    "            image[i].reverse()\n",
    "            for j in range(c):\n",
    "                image[i][j] ^= 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(image)\n",
    "        n = len(image[0])\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range((n+1)//2):\n",
    "                image[i][j], image[i][n-1-j] = 1-image[i][n-1-j], 1-image[i][j]\n",
    "\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        for i in range(n):\n",
    "            left, right = 0, n - 1\n",
    "            while left < right:\n",
    "                if image[i][left] == image[i][right]:\n",
    "                    image[i][left] ^= 1\n",
    "                    image[i][right] ^= 1\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if left == right:\n",
    "                image[i][left] ^= 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            m, n = 0, len(image[0])-1\n",
    "            while m <= n:\n",
    "                # xor_res值为1说明m、n位置的值不同，不相同不需要改变\n",
    "                # xor_res值为0说明m、n位置的值相同，相同需要改变，0变成1，1变成0\n",
    "                xor_res = image[i][m] ^ image[i][n]\n",
    "                if not xor_res:\n",
    "                    image[i][m] = image[i][n] = 0 if image[i][m] else 1\n",
    "                m += 1\n",
    "                n -= 1\n",
    "        return image                    \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        image = [img[::-1] for img in image]\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                image[i][j] = 1 - image[i][j]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        ans=[]\n",
    "        mem=[]\n",
    "        for i in image:\n",
    "            emm=i[::-1]\n",
    "            for item in emm:\n",
    "                mem.append(int(not bool(item)))\n",
    "            ans.append(mem)\n",
    "            mem=[]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\r\n",
    "        C=len(image[0])\r\n",
    "        for row in image:\r\n",
    "            l,r=0,C-1\r\n",
    "            while l<r:\r\n",
    "                if row[l]==row[r]:\r\n",
    "                    row[l]^=1\r\n",
    "                    row[r]=row[l]\r\n",
    "                l+=1\r\n",
    "                r-=1\r\n",
    "            if l==r:\r\n",
    "                row[l]^=1\r\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        l=[]\n",
    "        for i in image:\n",
    "            l.append(i[::-1])\n",
    "        for i in l:\n",
    "            for j in range(0,len(i)):\n",
    "                i[j] = 1 - i[j]\n",
    "            \n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        ans = [[0 for _ in range(n)] for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            ans[i] = [1 - x for x in image[i][::-1]]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        # 翻转每一行\n",
    "        for row in image:\n",
    "            row.reverse()\n",
    "            # 反转每个元素（0 变 1，1 变 0）\n",
    "            for i in range(len(row)):\n",
    "                row[i] = 1 - row[i]  # 如果元素是 0, 它变成 1. 如果它是 1, 它变成 0.\n",
    "\n",
    "        return image  # 直接返回修改后的原始图像列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\r\n",
    "        C=len(image[0])\r\n",
    "        for row in image:\r\n",
    "            l,r=0,C-1\r\n",
    "            while l<=r:\r\n",
    "                if l==r:\r\n",
    "                    row[l]=1-row[l]\r\n",
    "                else:\r\n",
    "                    row[l],row[r]=1-row[r],1-row[l]\r\n",
    "                l+=1\r\n",
    "                r-=1\r\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        m,n=len(image),len(image[0])\n",
    "        for i in range(m):\n",
    "            image[i]=image[i][::-1]\n",
    "            for j in range(n):\n",
    "                image[i][j]^=1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        i=0\n",
    "        for i in range(0,len(image)):\n",
    "            left=0\n",
    "            right=len(image)-1\n",
    "            while left <=right:\n",
    "                image[i][left],image[i][right]=1-image[i][right],1-image[i][left]\n",
    "                left+=1\n",
    "                right-=1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        ans=[]\n",
    "        for i in image:\n",
    "            ans.append([])\n",
    "            emm=i[::-1]\n",
    "            for item in emm:\n",
    "                ans[-1].append(int(not bool(item)))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        # 先水平翻转\n",
    "        for i in range(len(image)):\n",
    "            l, r = 0, len(image) -1\n",
    "            while l <= r:\n",
    "                if l != r:\n",
    "                    image[i][l], image[i][r] = image[i][r], image[i][l]\n",
    "                    image[i][l] = 0 if image[i][l] == 1 else 1\n",
    "                image[i][r] = 0 if image[i][r] == 1 else 1\n",
    "                l += 1\n",
    "                r -= 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        m , n = len(image) , len(image[0])\n",
    "        for i in image:\n",
    "            left , right = 0 , n - 1\n",
    "            while left <= right:\n",
    "                i[left] , i[right] = i[right] , i[left]\n",
    "                if left == right:\n",
    "                    i[left] = 1 if not i[left] else 0\n",
    "                else:\n",
    "                    i[left] = 1 if not i[left] else 0\n",
    "                    i[right] = 1 if not i[right] else 0\n",
    "                left += 1\n",
    "                right -= 1\n",
    "        return image\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i][:] = image[i][::-1]\n",
    "        for i in range(len(image)):\n",
    "            for j in range(len(image[0])):\n",
    "                image[i][j] = 1 - image[i][j]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(image), len(image[0])\n",
    "        for i in range(m):\n",
    "            image[i] = image[i][::-1]\n",
    "            for j in range(n):\n",
    "                image[i][j] = 0 if image[i][j] == 1 else 1 \n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        res = list()\n",
    "        for row in image:\n",
    "            res.append(list(1-x for x in row[::-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for row in image:\n",
    "            for j in range((len(row) + 1) // 2):\n",
    "                if row[j] == row[-1-j]:\n",
    "                    row[j] = row[-1-j] = 1 - row[j]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        for i in range(n):\n",
    "            image[i] = image[i][::-1]\n",
    "            for j in range(n):\n",
    "                image[i][j] = 1 - image[i][j]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        # 先水平翻转\n",
    "        for i in range(len(image)):\n",
    "            l, r = 0, len(image) -1\n",
    "            while l <= r:\n",
    "                if l == r:\n",
    "                    image[i][l] = 0 if image[i][l] == 1 else 1\n",
    "                else:\n",
    "                    image[i][l], image[i][r] = image[i][r], image[i][l]\n",
    "                    image[i][l] = 0 if image[i][l] == 1 else 1\n",
    "                    image[i][r] = 0 if image[i][r] == 1 else 1\n",
    "                l += 1\n",
    "                r -= 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        ans=list()\n",
    "        mem=list()\n",
    "        for i in image:\n",
    "            a=i[::-1]\n",
    "            for item in a:\n",
    "                mem.append(int(not bool(item)))\n",
    "            ans.append(mem)\n",
    "            mem=[]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        for i in range(n):\n",
    "            left, right = 0, n - 1\n",
    "            while left < right:\n",
    "                if image[i][left] == image[i][right]:\n",
    "                    image[i][left] ^= 1\n",
    "                    image[i][right] ^= 1\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if left == right:\n",
    "                image[i][left] ^= 1\n",
    "        return image\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        # 先水平翻转\n",
    "        for i in range(len(image)):\n",
    "            l, r = 0, len(image) -1\n",
    "            while l < r:\n",
    "                image[i][l], image[i][r] = image[i][r], image[i][l]\n",
    "                l += 1\n",
    "                r -= 1\n",
    "        # 再反转\n",
    "        for i in range(len(image)):\n",
    "            for j in range(len(image)):\n",
    "                image[i][j] = 0 if image[i][j] == 1 else 1\n",
    "        \n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        n = len(image)\n",
    "        for i in range(n):\n",
    "            left, right = 0, n - 1\n",
    "            while left < right:\n",
    "                if image[i][left] == image[i][right]:\n",
    "                    image[i][left] ^= 1\n",
    "                    image[i][right] ^= 1\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if left == right:\n",
    "                image[i][left] ^= 1\n",
    "        return image\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            image[i]=image[i][::-1]\n",
    "            for j in range(len(image[i])):\n",
    "                if image[i][j]==0:\n",
    "                    image[i][j]=1\n",
    "                else:\n",
    "                    image[i][j]=0\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        ans=[]\n",
    "        mem=[]\n",
    "        for i in image:\n",
    "            for item in i[::-1]:\n",
    "                mem.append(int(not bool(item)))\n",
    "            ans.append(mem)\n",
    "            mem=[]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        rows = len(image)\n",
    "        colos = len(image[0])\n",
    "        for row in range(rows):\n",
    "            image[row] = image[row][::-1]\n",
    "            for colo in range(colos):\n",
    "                image[row][colo] ^= 1\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        return [[j^1 for j in row[::-1]] for row in image]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        for i in range(len(image)):\n",
    "            row=image[i]\n",
    "            image[i]=[1-row[x] for x in range(len(row)-1,-1,-1)]\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        res=[]\n",
    "        for i in image:\n",
    "            res.append([abs(x-1) for x in reversed(i)])\n",
    "\n",
    "        #return res\n",
    "\n",
    "        return [[abs(x-1) for x in reversed(i)] for i in image]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        # horizontal flip\n",
    "        n = len(image)\n",
    "        mid = n //2 + n % 2\n",
    "        for i in range(n):\n",
    "            for j in range((n+1)//2):\n",
    "                image[i][j], image[i][n-1-j] = 1 - image[i][n-1-j], 1-image[i][j]\n",
    "\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        \n",
    "        for i in range(len(image)):\n",
    "            left, right = 0, len(image[0])-1\n",
    "            while left < right:\n",
    "                if image[i][left] == image[i][right]:\n",
    "                    image[i][left] = (image[i][left]+1)%2\n",
    "                    image[i][right] = (image[i][right]+1)%2\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if left == right:\n",
    "                image[i][left] = (image[i][left]+1)%2\n",
    "        return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:\n",
    "        m,n = len(image),len(image[0])\n",
    "        for i in range(m):\n",
    "            image[i] = image[i][::-1]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                image[i][j] = abs(image[i][j]-1)\n",
    "        return image"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
